package com.crh.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import com.crh.service.IGoodsService;

/**
 * 注册Service层的工厂单例模式
 *
 * @author chaui
 *
 */
public class ServiceFactory {
	// 商品的服务层
	private static IGoodsService iGoodsService = null;
	private static ServiceFactory serviceInstance = new ServiceFactory();

	private ServiceFactory() {
		try {
			// 利用配置文件读取
			Properties prop = new Properties();
			// 当前的Class加载配置文件
			InputStream in = ServiceFactory.class.getClassLoader()
					.getResourceAsStream("config/serviceconfig.properties");
			prop.load(in);
			/*****************************
			 * iGoodsServiceImpl
			 *******************************/
			// 读取键值对
			String iGoodsServiceImplClass = prop.getProperty("iGoodsServiceImplClass");
			Class iGoodsServiceImplClazz = Class.forName(iGoodsServiceImplClass);
			iGoodsService = (IGoodsService) iGoodsServiceImplClazz.newInstance();
		} catch (Exception e) {
			throw new ExceptionInInitializerError(e);
		}
	}

	public static IGoodsService getiGoodsService() {
		return iGoodsService;
	}

	public static ServiceFactory getServiceInstance() {
		if (serviceInstance == null) {
			serviceInstance = new ServiceFactory();
		}
		return serviceInstance;
	}


}
